package com.hs.panghu.servlet;

import com.alibaba.fastjson.JSON;
import com.hs.panghu.dao.CommunityDao;
import com.hs.panghu.dao.imp.CommunityDaoImpl;
import com.hs.panghu.entity.Community;
import com.hs.panghu.util.Result;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@WebServlet("/community")
public class CommunityServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String action = req.getParameter("action");
        CommunityDao communityDao = new CommunityDaoImpl();
        if("communityList".equals(action)){  // 分页查询
            Map<String,String> map = new HashMap<>();
            String name = req.getParameter("name");
            String startTime = req.getParameter("startTime");
            String endTime = req.getParameter("endTime");
            String pageNum = req.getParameter("pageNum");
            String pageSize = req.getParameter("pageSize");
            map.put("name",name);
            map.put("startTime",startTime);
            map.put("endTime",endTime);
            map.put("pageNum",pageNum);
            map.put("pageSize",pageSize);

            // 连接数据库
            List<Community> list = communityDao.queryCommunitys(map);

//            动态获取社区的数据总条数
            int total = communityDao.queryCounts(map);
//            创建一个返回值的map集合，用来包装list数据和total总数量
            Map resultMap = new HashMap();
            resultMap.put("list",list);
            resultMap.put("total",total);
//            把结果集合,封装到result对象中
            Result success = Result.success(resultMap);
            resp.getWriter().write(JSON.toJSONString(success));
        }else if( "communityUpdate".equals(action)){  // 增加或修改
            String flag = req.getParameter("addOrUpdateFlag");
            String communityAddress = req.getParameter("communityAddress");
            String communityAreaCode = req.getParameter("communityAreaCode");
            if(communityAreaCode.equals("")){
                communityAreaCode="0";
            }
            String communityCity = req.getParameter("communityCity");
            String communityCoordinates = req.getParameter("communityCoordinates");
            String communityDistrict = req.getParameter("communityDistrict");
            String communityId = req.getParameter("communityId");
            if(communityId.equals("")){
                communityId="0";
            }
            String communityLatitude = req.getParameter("communityLatitude");
            String communityLongitude = req.getParameter("communityLongitude");
            String communityName = req.getParameter("communityName");
            String communityProvince = req.getParameter("communityProvince");
            // 组成对象
            Community community = new Community(
                    Integer.parseInt(communityId),
                    communityName,
                    communityProvince,
                    communityCity,
                    communityDistrict,
                    communityAddress,
                    communityLatitude,
                    communityLongitude,
                    Integer.parseInt(communityAreaCode),
                    communityCoordinates
            );

            if("1".equals(flag)){  //   增加
                int row = communityDao.addCommunity(community);
                if(row!=0){
                    Result success = Result.success();
                    resp.getWriter().write(JSON.toJSONString(success));
                }else {
                    Result error = Result.error();
                    resp.getWriter().write(JSON.toJSONString(error));
                }
            }else{
                int row = communityDao.editCommunity(community);
                if(row!=0){
                    Result success = Result.success();
                    resp.getWriter().write(JSON.toJSONString(success));
                }else {
                    Result error = Result.error();
                    resp.getWriter().write(JSON.toJSONString(error));
                }
            }
        }else if ("deleteCommunity".equals(action)){
            //执行删除操作
//            System.out.println("到达删除小区");
            String communityId = req.getParameter("communityId");
//            System.out.println("拿到的删除ID为："+communityId);
            int row = communityDao.delCommunity(Integer.valueOf(communityId));
            if(row!=0){
                Result success = Result.success();
                resp.getWriter().write(JSON.toJSONString(success));
            }else {
                Result error = Result.error();
                resp.getWriter().write(JSON.toJSONString(error));
            }
        }
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req,resp);
    }
}
